package com.jsu.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@RefreshScope
@Configuration
@ConfigurationProperties(prefix = "alipay")
@Data
/***
 * @author: hjt
 * @Date: 2020/11/13/19:19
 * @Description: 支付宝配置类(读取配置文件)
 */
public class MyAliPayConfig {
   /**
    * APPID
    */
   @Value("${alipay.appId}")
   private String appId;

   /**
    * 商户私钥, 即PKCS8格式RSA2私钥
    */
   @Value("${alipay.appPrivateKey}")
   private String privateKey;

   /**
    * 支付宝公钥
    */
   @Value("${alipay.alipayPublicKey}")
   private String publicKey;

   /**
    * 服务器异步通知页面路径,需http://格式的完整路径
    * 踩坑:不能加?type=abc这类自定义参数
    */
   @Value("${alipay.notifyUrl}")
   private String notifyUrl;

   @Value("${alipay.serverUrl}")
   private String serverUrl;

   @Bean
   public AlipayClient alipayClient() throws AlipayApiException {
      // 2.把需要配置的参数set进AlipayConfig类中
      AlipayConfig alipayConfig = new AlipayConfig();
      alipayConfig.setServerUrl(serverUrl);
      alipayConfig.setAppId(appId);
      alipayConfig.setPrivateKey(privateKey);
      alipayConfig.setFormat("json");
      alipayConfig.setAlipayPublicKey(publicKey);
      alipayConfig.setCharset("UTF8");
      alipayConfig.setSignType("RSA2");
      return new DefaultAlipayClient(alipayConfig);
   }
}
